package xycdao

import (
	"Cgo/front/models"
	"Cgo/global"
)

type xycCartDao struct{}

var XycCartDao = new(xycCartDao)

// 添加购物车
func (*xycCartDao) AddCartDao(cart models.XycCart) error {
	if err := global.DB.Create(&cart).Error; err != nil {
		return err
	}
	return nil
}

// 根据id删除购物车
func (*xycCartDao) DeleteCartByIdDao(id int) error {
	if err := global.DB.Delete(&models.XycCart{}, id).Error; err != nil {
		return err
	}
	return nil
}

// 根据userId查询购物车列表
func (*xycCartDao) FindCartByUserIdDao(userId int) ([]models.XycCart, error) {
	var carts []models.XycCart
	if err := global.DB.Where("user_id = ?", userId).Find(&carts).Error; err != nil {
		return carts, err
	}
	return carts, nil
}

// 修改购物车
func (*xycCartDao) UpdateCartDao(cart models.XycCart) error {
	if err := global.DB.Save(&cart).Error; err != nil {
		return err
	}
	return nil
}
